﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Reborn.Infrastructure.Extensions;

namespace SimpleSpider.Framework.Downloader
{
    public class HttpDownloader : IDownloader
    {
        HttpClientHandler _httpClientHandler = null;
        HttpClient _httpClient = null;
        public HttpDownloader()
        {
            _httpClientHandler = new HttpClientHandler();
            _httpClientHandler.UseCookies = false;
            _httpClient = new HttpClient(_httpClientHandler);
        }

        public override string Get(string url)
        {
            return _httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
        }

        public override void SetProxyIP(string ip, string port)
        {
            if (string.IsNullOrEmpty(ip)) return;

            _httpClientHandler.Proxy = new WebProxy(ip, Convert.ToInt32(port));
        }

        /// <summary>
        /// 设置头信息
        /// </summary>
        /// <param name="headerString"></param>
        public override void SetRequest(string headerString)
        {
            _httpClient.SetRequest(headerString);
        }
    }
}
